// 导入request.js请求工具
import request from '@/utils/request.js';

// 新增新闻
export function addNews(news) {
    return request({
        url: '/admins/news/addNews',
        method: 'post',
        data: {
            title: news.title,
            summary: news.summary,
            author: news.author,
            coverImage: news.coverImage,
            content: news.content,
            createTime: new Date().toISOString()
        }
    });
}

// 封面图片上传
export const uploadCoverImg = (file) => {
    const formData = new FormData();
    formData.append('file', file);
    return request({
      url: '/admins/uploadCoverImg',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  };

// 获取新闻数据
export const getNews = ({ pageNum, pageSize, sortType, startDate, endDate, searchText }) => {
    return request({
        url: '/admins/news/getNews',
        method: 'get',
        params: {
            pageNum,
            pageSize,
            sortType,
            startDate,
            endDate,
            searchText
        }
    });
};

// 添加删除新闻的API调用
export const deleteNews = (nid) => {
    return request({
        url: `/admins/news/deleteNews/${nid}`,
        method: 'delete'
    });
};

// 获取单个新闻详情
export const getNewsDetail = (nid) => {
    return request({
        url: `/admins/news/getNewsDetail/${nid}`,
        method: 'get'
    });
};

// 更新新闻
export function updateNews(nid, news) {
    return request({
        url: `/admins/news/updateNews/${nid}`,
        method: 'put',
        data: {
            title: news.title,
            summary: news.summary,
            author: news.author,
            coverImage: news.coverImage,
            content: news.content
        }
    });
}
